/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Realtime.board;

import BUS.Group_userBUS;
import BUS.SubConfig;
import DTO.Group_user;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author quangphuc
 */
@WebServlet(name = "groupuser", urlPatterns = {"/groupuser"})
public class groupuser extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */            
            response.addHeader("Access-Control-Allow-Origin", "*");
            String fn = request.getParameter("fn");
            System.out.println("demo fn : " + fn);
            if(fn.equals("0")){//thuc hien inser du lieu
                String name = request.getParameter("name");
                String note = request.getParameter("note");                                
                Group_user item = new Group_user();
                item.setId(1);
                item.setGroup_name(name);
                item.setNote(note);
                long id = this.Insert(item);
                String json = "" + id;
                out.println(json);
            }
            else if(fn.equals("1")){// thuc hien update
                String strId = request.getParameter("id");
                String name = request.getParameter("name");
                String note = request.getParameter("note");                
                long id = 0;
                try{
                    id = Long.parseLong(strId);
                }
                catch(Exception error){}
                Group_user item = new Group_user();                
                item.setId(id);
                item.setGroup_name(name);
                item.setNote(note);
                long rs = this.Update(item);
                String json = "" + rs;
                out.println(json);
            }
            else if(fn.equals("2")){//thuc hien delete
                String strId = request.getParameter("id");
                long id = 0;
                try{
                    id = Long.parseLong(strId);
                }
                catch(Exception error){}
                Group_user item = new Group_user();                
                item.setId(id);
                long rs = this.Delete(item);
                String json = "" + rs;
                out.println(json);
            }
            else{
                out.println(getGroupuser());
            }
        }
    }
    
    protected String getGroupuser(){
        Group_userBUS item = new Group_userBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        List<Group_user> list = item.getAll(config);
        String json = item.ListToJson("Group_user", list);
        System.out.println("json : " + json);
        config.close();
        return json;
    }
    
    protected long Insert(Group_user item){
        Group_userBUS guBUS = new Group_userBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        long id =(long)guBUS.insertOnSubmit(item, config);
        item.setId(id);
        config.close();
        return id;
    }
    
    protected  long Update(Group_user item) {
        Group_userBUS guBUS = new Group_userBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        long id =(long)guBUS.updateOnSubmint(item, config);
        item.setId(id);
        return id;
    }
    
    protected long Delete(Group_user item){
        Group_userBUS guBUS = new Group_userBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        long id =(long)guBUS.deleteOnSubmint(item, config);
        item.setId(id);
        return id;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
